package com.neusoft.databus.policy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.exception.CustomRuntimeException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.constant.DatabusErrorCode;
import com.neusoft.databus.policy.converter.ExpressionConverter;
import com.neusoft.databus.policy.dto.ExpressionDTO;
import com.neusoft.databus.policy.model.Expression;
import com.neusoft.databus.policy.repository.ExpressionRepository;
import com.neusoft.databus.policy.service.ExpressionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 策略表达式管理控制器
 *
 * @author naxiang
 */
@Api("策略表达式管理控制器")
@RestController
@RequestMapping(value = "/api/w/expressions")
public class ExpressionController {

    @Autowired
    private ExpressionRepository expressionRepository;
    @Autowired
    private ExpressionConverter expressionConverter;
    @Autowired
    private ExpressionService expressionService;

    @ApiOperation("查询所有策略表达式")
    @GetMapping
    public ResultListDTO<ExpressionDTO> index(final Searchable searchable) {
        final List<Expression> expressions = this.expressionRepository.search(searchable);
        final ResultListDTO<ExpressionDTO> result =
                ResultListDTO.success(this.expressionConverter.toListDTO(expressions));
        return result;
    }

    @ApiOperation("分页查询所有策略表达式")
    @GetMapping("/p")
    public PageResultDTO<ExpressionDTO> search(final Searchable searchable, final Pageable pageable) {
        final Page<Expression> models = this.expressionRepository.search(pageable, searchable);
        return PageResultDTO.success(this.expressionConverter.toPageDTO(models));
    }

    /**
     * 创建策略表达式
     *
     * @param dto
     * @return
     */
    @ApiOperation("创建策略表达式")
    @PostMapping
    public ResultDTO<ExpressionDTO> create(@RequestBody ExpressionDTO dto) {
        final Expression model = this.expressionConverter.toModel(dto);
        final ExpressionDTO result = this.expressionConverter.toDTO(this.expressionService.create(model));
        return ResultDTO.success(result);
    }

    /**
     * 更新表达式
     *
     * @param code 策略编码
     * @param dto 策略DTO
     * @return ResultDTO<ResourceDTO>
     * @throws CustomRuntimeException
     */
    @ApiOperation(value = "更新表达式", notes = "仅更新表达式的基本属性")
    @PutMapping("/{id}")
    public ResultDTO<ExpressionDTO> update(@PathVariable Long id, @RequestBody ExpressionDTO dto) {
        Expression model = this.expressionRepository.getOne(id);
        if (null == model) {
            throw new CustomRuntimeException(DatabusErrorCode.ERR_CODE_404001, new Object[] {id });
        }
        // 强制忽略传递的ID
        dto.setId(model.getId());
        model = this.expressionConverter.toModel(dto);
        this.expressionService.update(model);
        final ExpressionDTO result = this.expressionConverter.toDTO(model);
        return ResultDTO.success(result);
    }
}
